package com.shuzijun.leetcode.editor.cn;
public class ColorFillLcci{
    public static void main(String[] args) {
         
         Solution solution = new ColorFillLcci().new Solution();
        
        
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        int n = image.length;
        int m = image[0].length;
        dfs(image,n,m,sr,sc,image[sr][sc],newColor);
        return image;
    }

        private void dfs(int[][] image, int n, int m, int sr, int sc, int color, int newColor) {
            image[sr][sc] = newColor;
            int[][] dirs = {{-1,0},{1,0},{0,1},{0,-1}};
            for (int k = 0; k < 4; k++) {
                int newr = sr + dirs[k][0];
                int newc = sc + dirs[k][1];
                if (newr < 0 || newr >= n || newc < 0|| newc >= m ||
                image[newr][newc] != color || image[newr][newc] == newColor){
                    continue;
                }
                dfs(image,n,m,newr,newc,color,newColor);

            }
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}